<?php

namespace backend\controllers;

/**
 * @author  shuang
 * @date    2016-12-22 22:44:26
 * @version V1.0
 * @desc    
 */
use common\components\BController;
use common\components\MTools;
use yii\helpers\Url;
use yii\helpers\Json;
use Yii;
use yii\helpers\ArrayHelper;
use backend\models\MY_UserPayRecord;
use backend\models\MY_UserPayRecordHy;
use backend\models\MY_UserOrder;

class FinansysController extends BController {

    public function actions() {
        return [
            "tenantlist" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new MY_UserPayRecord(),
                "renderTo" => "tenantlist"
            ],
            "rechargelist" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new MY_UserOrder(),
                "renderTo" => "rechargelist"
            ],
            "brushlist" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new MY_UserPayRecordHy(),
                "renderTo" => "brushlist"
            ],
            "drawallist" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new \backend\models\MY_UserDrawalRecord(),
                "renderTo" => "drawallist"
            ],
            "profitlist" => [
                "class" => "\common\actions\ListAction",
                "modelClass" => new \backend\models\MY_UserPayRecord(),
                "renderTo" => "profitlist"
            ]
        ];
    }

    /*
     * ajax数据列表
     */

    public function actionAjaxtenantlist() {
        $res = MY_UserPayRecord::getMerList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $temp["rows"][] = [
                    "username" => $item["username"],
                    "desc" => ArrayHelper::getValue($item, "description"),
                    "amount" => "￥" . Yii::$app->formatter->asDecimal(ArrayHelper::getValue($item, "amount")),
                    "type" => in_array($item["class_type"], [1, 5, 8]) ? '<span class="label label-success">充值</span>' : (in_array($item["class_type"], [4, 6, 7, 9]) ? '<span class="label label-info">付款</span>' : '<span class="label label-danger">退款</span>'),
                    "time" => Yii::$app->formatter->asDatetime(ArrayHelper::getValue($item, "created_at"))
                ];
            }
        }
        echo Json::encode($temp);
    }

    public function actionAjaxbrushlist() {
        $res = MY_UserPayRecordHy::getBrushList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $temp["rows"][] = [
                    "username" => $item["username"],
                    "desc" => ArrayHelper::getValue($item, "description"),
                    "amount" => "￥" . Yii::$app->formatter->asDecimal(ArrayHelper::getValue($item, "amount")),
                    "type" => in_array($item["class_type"], [1, 2]) ? '<span class="label label-success">充值</span>' : (in_array($item["class_type"], [3]) ? '<span class="label label-danger">退款</span>' : null),
                    "time" => Yii::$app->formatter->asDatetime(ArrayHelper::getValue($item, "created_at"))
                ];
            }
        }
        echo Json::encode($temp);
    }

    public function actionAjaxrechargelist() {
        $res = MY_UserOrder::getList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $temp["rows"][] = [
                    "id" => $item["id"],
                    "username" => $item["username"],
                    "isupgrade" => ArrayHelper::getValue($item, "isupgrade"),
                    "status" => \frontend\models\WB_UserOrder::$order_status[$item["status"]],
                    "amount" =>abs(Yii::$app->formatter->asDecimal(ArrayHelper::getValue($item, "amount"))),
                    "time" => Yii::$app->formatter->asDatetime(ArrayHelper::getValue($item, "created_at"))
                ];
            }
        }
        echo Json::encode($temp);
    }

    public function actionAjaxdrawallist() {
        $res = \backend\models\MY_UserDrawalRecord::getList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $temp["rows"][] = [
                    "username" => $item["username"],
                    "partner_trade_no" => ArrayHelper::getValue($item, "partner_trade_no"),
                    "payment_no" => ArrayHelper::getValue($item, "payment_no"),
                    "amount" => abs(Yii::$app->formatter->asDecimal(ArrayHelper::getValue($item, "amount"))),
                    "payment_time" => ArrayHelper::getValue($item, "payment_time")
                ];
            }
        }
        echo Json::encode($temp);
    }

    public function actionAjaxprofitlist() {
        $res = \backend\models\MY_UserPayRecord::searchProfitList();
        $temp = ["total" => $res["total"], "rows" => []];
        if ($res["total"] > 0) {
            foreach ($res["data"] as $item) {
                $temp["rows"][] = [
                    "id"=>$item["id"],
                    "class_type"=>  MY_UserPayRecord::$class_type_note[$item["class_type"]],
                    "amount"=>abs(Yii::$app->formatter->asDecimal($item["amount"])),
                    "description"=>Yii::$app->formatter->asText($item["description"]),
                    "created_at"=>Yii::$app->formatter->asDatetime($item["created_at"])
                ];
            }
        }
        echo Json::encode($temp);
    }

}
